<template>
  <div class="back_img">
    <!-- 添加返回按钮 -->
    <div class="back-button" @click="goBack">
      <span class="back-icon">返回</span>
    </div>
    <div class="dadwafgada" style="padding-top: 100px; display: flex; justify-content: center">
      <text class="awdawdfa">社区大舞台抽奖</text>
    </div>
    <div style="padding: 60px">
      <div style="margin-top: 200px; width: 100%; background-color: #fff; border-radius: 10px">
        <div style="padding: 5px">
          <div class="adawdedaw">
            <div v-for="item in prize" class="dawddwdZz" style="border-radius: 10px; border: 2px solid orange">
              <image :src="'http://www.wantongkonggu.cn/' + item.prizeIcon" style="width: 100%; border-radius: 10px" mode="widthFix"></image>
              <div style="text-align: center; font-size: 12px; color: red">{{ item.prizeName }}</div>
            </div>
          </div>
        </div>
      </div>
      <div :class="iadawdawdfa ? 'buttonmdadawd' : 'buttonmda'" @click="addUser">{{ iadawdawdfa ? "等待开奖..." : "发送弹幕参与抽奖！" }}</div>
    </div>
    <u-modal title="友情提示" :show="isshjjopawdaw" confirmText="去看看其他活动" @confirm="adwdfgadw" :showCancelButton="false"> 当前抽奖活动已经截止！ </u-modal>
    <u-modal title="友情提示" :show="adgadwawgead" confirmText="确定" @confirm="adwdfgadwdwad" :showCancelButton="false"> 恭喜您抽中{{ fadgeafdaw.prizeName }},等待发放！ </u-modal>

    <u-popup :show="isShow" :round="10" mode="bottom">
      <view style="padding: 20px 20px 20px 20px">
        <!--     <div style="float: right; color: #999; font-size: 12px" @click="rfgewawdaw">暂不设置</div> -->
        <div style="font-weight: bold; margin-top: 15px; letter-spacing: 1.5px">设置您的昵称与头像</div>
        <div style="font-size: 12px; padding: 5px 0px 10px 0px; border-bottom: 1px solid #ededed">用户抽奖奖品兑换认证</div>
        <div style="display: flex; height: 60px; font-size: 13px; align-items: center; border-bottom: 1px solid #ededed">
          <div style="width: 60px">头像</div>
          <div>
            <button open-type="chooseAvatar" class="dawdaw" @chooseavatar="onChooseAvatar">
              <image :src="newAvatar.indexOf('http') != -1 ? newAvatar : 'http://www.wantongkonggu.cn/' + newAvatar" style="width: 40px; height: 40px"></image>
            </button>
          </div>
        </div>
        <div style="display: flex; height: 60px; font-size: 13px; align-items: center; border-bottom: 1px solid #ededed">
          <div style="width: 60px">昵称</div>
          <div>
            <input type="nickname" v-model="nickname" placeholder="请输入昵称" />
          </div>
        </div>
        <div style="margin-top: 15px; display: flex; justify-content: center">
          <button class="dawdjnhuhie" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">保 存</button>
        </div>
      </view>
    </u-popup>
  </div>
</template>

<script>
import { activitylist, prizelist, participate, participateList, notBindingPhone } from "../../utils/api/api.js";
import { userEdit } from "../../utils/api/local";
import { programlogin, phoneAndPassword, getUserInfo, getUserPhoneNumber, newUserIntegral } from "../../utils/request_api.js";
export default {
  data() {
    return {
      prizeIds: "",
      barrage: "",
      isShow: false,
      isshjjopawdaw: false,
      dawdawfga: null,
      fadgeafdaw: {},
      adgadwawgead: false,
      iadawdawdfa: false,
      authKey: "",
	  dawdadwavf: false,
      nickname: "微信用户",
      newAvatar: "https://www.wantongkonggu.cn/crmebimage/profile/upload/2025/03/20/f_20250320164147A002.png",
      prize: [
        {
          name: "扫地机",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/寻找洗地机照片_20251020150549A015.png",
        },
        {
          name: "保温杯一个",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成杯子图片_20251020150744A016.png",
        },
        {
          name: "现金红包1元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
        {
          name: "现金红包10元",
          icon: "http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/20/生成红包封面缩略图_20251020150943A017.png",
        },
      ],
    };
  },
  async mounted() {
    this.getData();
    let storedOpenID = uni.getStorageSync("openID");
    /*  if (storedOpenID) { */
    this.initUser();
    /*  } else {
      this.autoLogin();
    } */
  },
  methods: {
    // 添加返回方法
    goBack() {
      uni.switchTab({ url: "/pages_h5/index_two/index" });
    },
    adwdfgadwdwad() {
      uni.redirectTo({
        url: "/pages_h5/myCenter/components/prizelist",
      });
    },
    adwdfgadw() {
      uni.switchTab({
        url: "/pages_h5/index_two/index",
      });
    },
    rfgewawdaw() {
      this.isShow = false;
    },
    onChooseAvatar(e) {
      console.log(e);
      var that = this;
      uni.uploadFile({
        url: "https://www.wantongkonggu.cn//api/frontApph7/frontCommon/uploadTuPian?model=maintain&pid=0",
        filePath: e.detail.avatarUrl,
        name: "multipart",
        formData: {
          model: "maintain",
          pid: 0,
        },
        success: function (uploadRes) {
          const data = JSON.parse(uploadRes.data);
          that.newAvatar = data.fileName;
        },
        fail: function (err) {
          console.error("上传失败", err);
        },
      });
    },
    getPhoneNumber(e) {
      if (e.detail.code && e.detail.errMsg == "getPhoneNumber:ok") {
        getUserPhoneNumber(e.detail).then((res) => {
          if (res.code == 200 && res.data.errmsg == "ok") {
            let that = this;
            this.account = res.data.phone_info.phoneNumber;
			if(this.dawdadwavf) {
				this.awdawgawdgrr();
				this.isShow = false;
			} else {
				 this.gotoLogin();
			}
          } else {
            console.log("🚀 ~ getPhoneNumber ~ res:", res);
            uni.showToast({
              title: res.message,
              icon: "none",
            });
          }
        });
      }
    },
    gotoLogin() {
      // var params = {
      //   phone: this.account,
      //   password: "",
      //   captcha: "",
      //   key: this.authKey,
      //   type: "routine",
      //   codeOrPassword: "3",
      //   inviterLinkId: "",
      //   inviteeType: "",
      // };
      var params = {
        key: this.authKey,
        type: "routine",
        openId: this.openid,
        phone: "",
        password: "",
        captcha: "",
        codeOrPassword: "3",
        inviterLinkId: "",
      };
      notBindingPhone(params).then((res) => {
        if (res.code == 200) {
          const data = res.data;
          uni.setStorageSync("uid", data.uid);
          uni.setStorageSync("token", data.token);
          uni.setStorageSync("userInfo_data", data);
          uni.setStorageSync("openID", this.authKey);
          this.initUser();
          this.awdawgawdgrr();
          this.isShow = false;
        }
      });
      // phoneAndPassword(params).then((res1) => {
      //   if (res1.code == 200) {
      //     uni.setStorageSync("userInfo_data", res1.data);
      //     this.uid = res1.data.uid;
      //     uni.setStorageSync("uid", res1.data.uid);
      //     uni.setStorageSync("token", res1.data.token);
      //     uni.setStorageSync("openID", res1.data.key);
      //     this.initUser();
      //     this.awdawgawdgrr();
      //     this.isShow = false;
      //   }
      // });
    },

    awdawgawdgrr() {
      if (this.nickname == "") {
        uni.showToast({
          icon: "none",
          title: "请填写完整！",
        });
        return;
      }
      var params = {
        avatar: this.newAvatar,
        nickname: this.nickname,
        phone: this.account,
        account: this.account,
      };
      userEdit(params).then((res) => {
        this.initUser();
      });
    },
    initUser() {
      getUserInfo().then((res) => {
        if (res.code == 401) {
          this.autoLogin();
        } else {
          this.userInfo = res.data;
          if (this.userInfo.phone == null || this.userInfo.phone == "") {
            this.isShow = true;
			this.dawdadwavf = true
          }
          uni.setStorageSync("userInfo", res.data);
          // this.awdawgawdgrr()
        }
      });
    },
    autoLogin() {
      var that = this;
      uni.login({
        provider: "weixin",
        success: (res) => {
          if (res.code) {
            that.wechatLogin(res.code);
          } else {
            console.error("登录失败:", res.errMsg);
            uni.showToast({
              title: "微信登录失败，请稍后再试",
              icon: "none",
            });
          }
        },
        fail: (err) => {
          console.error("登录失败:", err);
          uni.showToast({
            title: "微信登录失败，请稍后再试",
            icon: "none",
          });
        },
      });
    },

    wechatLogin(code) {
      var that = this;
      wx.getUserInfo({
        success(res) {
          const { nickName, avatarUrl, gender } = res.userInfo;
          programlogin({ code, avatar: avatarUrl, nickName, sex: gender }).then((response) => {
            console.log("🚀 ~ success ~ response:", response);
            const data = response.data;
            that.authKey = data.key;
            that.openid = data.openId;
            if (data.type == "login") {
              uni.setStorageSync("uid", data.uid);
              uni.setStorageSync("token", data.token);
              uni.setStorageSync("userInfo_data", data);
              uni.setStorageSync("openID", data.key);
              that.initUser();
              // that.initIsC()
            } else {
              that.isShow = true;
            }
          });
        },
        fail: () => {
          uni.showToast({
            title: "您需要授权，才能获取您的信息！",
          });
          this.showLogin = true;
        },
      });
    },
    addUser() {
      if (!this.iadawdawdfa) {
        getUserInfo().then((res) => {
          if (res.code == 401) {
            this.autoLogin();
          } else {
            var prams = {
              ebLotteryActivityId: this.hdID,
              ebUserId: uni.getStorageSync("uid"),
              barrage: this.barrage,
            };
            participate(prams).then((res) => {
              uni.showToast({
                title: "参与成功！等待开奖。。",
                icon: "none",
              });
              this.initIsC();
            });
          }
        });
      }
    },
    getData() {
      activitylist({ isOpen: 0 }).then((res) => {
        if (res.code == 200) {
          if (res.rows.length > 0) {
            this.prizeIds = res.rows[0].ebLotteryActivityPrizeIds;
            this.hdID = res.rows[0].id;
            this.barrage = res.rows[0].barrageSetting;
            this.nickname = res.rows[0].activityInfo || "微信用户";
            this.getPrizeData();
            // this.inadwdawfa()
            this.dawdawfga = setInterval(() => {
              this.inadwdawfa();
            }, 2000);
            // this.initIsC()
          } else {
            this.isshjjopawdaw = true;
          }
        }
      });
    },
    inadwdawfa() {
      if (uni.getStorageSync("uid")) {
        var params = {
          ebLotteryActivityId: this.hdID,
          ebUserId: uni.getStorageSync("uid"),
        };
        participateList(params).then((res) => {
          if (res.code == 200) {
            if (res.rows.length > 0) {
              this.iadawdawdfa = true;
              if (res.rows[0].ebLotteryActivityPrize) {
                this.fadgeafdaw = res.rows[0].ebLotteryActivityPrize;
                this.adgadwawgead = true;
                clearInterval(this.dawdawfga);
              }
            }
          }
        });
      }
    },
    initIsC() {
      var params = {
        ebLotteryActivityId: this.hdID,
        ebUserId: uni.getStorageSync("uid"),
      };
      participateList(params).then((res) => {
        if (res.code == 200) {
          if (res.rows.length > 0) {
            this.iadawdawdfa = true;
          }
        }
      });
    },
    getPrizeData() {
      prizelist({ ids: this.prizeIds }).then((res) => {
        this.prize = res.rows;
      });
    },
  },
};
</script>

<style>
page {
  width: 100%;
  height: 100%;
}
.dawddwdZz {
  width: 30%;
  /* margin: 5px; */
}
/* 添加返回按钮样式 */
.back-button {
  position: absolute;
  top: 100rpx;
  left: 30rpx;
  z-index: 999;
  width: 110rpx;
  height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
}
.back-icon {
  color: #fff;
  font-family: "iconfont" !important;
  font-size: 30rpx;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.dawdjnhuhie {
  height: 30px;
  line-height: 30px;
  color: #fff;
  width: 100px;
  background-color: #ff5e0b;
  text-align: center;
}
.adawdedaw {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 5px;
}
.back_img {
  width: 100%;
  height: 100%;
  background: url("http://www.wantongkonggu.cn/crmebimage/profile/upload/2025/10/21/9999_20251021093524A047.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
}
.dadwafgada {
  transform: rotate(-5deg);
}
.awdawdfa {
  font-family: "ruizhi";
  font-size: 40px;
  color: #fff;
  letter-spacing: 5px;
  text-stroke: 4px #f92a4c;
  color: transparent;
  -webkit-background-clip: text;
  background-image: linear-gradient(to top, #fff, #fdff6e);
}
.buttonmda {
  width: 100%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background-color: orange;
  color: #fff;
  margin-top: 10px;
  border-radius: 10px;
}
.buttonmdadawd {
  width: 100%;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background-color: #999;
  color: #fff;
  margin-top: 10px;
  border-radius: 10px;
}
</style>
